package acme.bloodtime.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import modelo.ICiudadDO;
import modelo.IFamiliaDO;
import modelo.IPropiedadVampiroDAO;
import modelo.IVampiroArticuloDAO;
import modelo.IVampiroDAO;
import modelo.IVampiroDO;
import modelo.IVisitaDAO;
import dao.api.DataObject;
import dao.api.FactoryDAO;
import dao.api.Reference;

/**
 * 
 * @author Cesar A. Fuguet T.
 * 
 */
public class VampiroDAO extends BloodTimeBaseDAO implements IVampiroDAO {

    public VampiroDAO() {
        // Empty
    }

    @Override
    public void createTable() throws SQLException {
        StringBuffer strbuf;

        // ----------------------------------------

        strbuf = new StringBuffer();

        // ------------------------------------------------------------------
        // It could be a good practice to implement the method getTableName
        // in BaseDAO and eliminate the DAO part to generalize the method.
        // ------------------------------------------------------------------

        dropTable();

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());

        // ----------------------------------------

        strbuf = new StringBuffer();

        strbuf.append("CREATE TABLE ");
        strbuf.append(getTableName());
        strbuf.append(" (");
        strbuf.append(VampiroDO.ID);
        strbuf.append(" SERIAL PRIMARY KEY,    ");
        strbuf.append(VampiroDO.NOMBRE);
        strbuf.append(" VARCHAR(25) NOT NULL,    ");
        strbuf.append(VampiroDO.APELLIDO);
        strbuf.append(" VARCHAR(25) NOT NULL,    ");
        strbuf.append(VampiroDO.CORREO);
        strbuf.append(" VARCHAR(25) NOT NULL,    ");
        strbuf.append(VampiroDO.FECHA_NAC);
        strbuf.append(" DATE,    ");
        strbuf.append(VampiroDO.CONTRASENIA);
        strbuf.append(" VARCHAR(12) NOT NULL,    ");
        strbuf.append(VampiroDO.ALIAS);
        strbuf.append(" VARCHAR(25) NOT NULL,    ");
        strbuf.append(VampiroDO.IMAGEN);
        strbuf.append(" BYTEA,    ");
        strbuf.append(VampiroDO.FUERZA);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.DEFENSA);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.SIGILOSIDAD);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.VELOCIDAD);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.HAB_ARMAS);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.HAB_NEGOCIOS);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.PTOS_ENTRENAMIENTO);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.PTOS_EXPERIENCIA);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.SALUD);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.SANGRE);
        strbuf.append(" INT NOT NULL,    ");
        strbuf.append(VampiroDO.DINERO);
        strbuf.append(" NUMERIC NOT NULL,    ");
        strbuf.append(VampiroDO.NUM_ESCLAVOS);
        strbuf.append(" NUMERIC NOT NULL,    ");
        strbuf.append(VampiroDO.ACTIVO);
        strbuf.append(" BOOLEAN NOT NULL,    ");
        strbuf.append(VampiroDO.FAMILIA_ID);
        strbuf.append(" INT REFERENCES ");

        FamiliaDAO familiaDAO = new FamiliaDAO();
        strbuf.append(familiaDAO.getTableName());

        strbuf.append(", ");

        strbuf.append(VampiroDO.CIUDAD_ID);
        strbuf.append(" INT REFERENCES ");

        CiudadDAO ciudadDAO = new CiudadDAO();
        strbuf.append(ciudadDAO.getTableName());

        // strbuf.append(" NOT NULL");
        strbuf.append(")");

        System.err.println(strbuf.toString());

        connection.createStatement().execute(strbuf.toString());
    }

    @Override
    public void delete(DataObject dataObject) throws SQLException {
        internalDelete(dataObject, VampiroDO.class);
    }

    @Override
    public void insert(DataObject dataObject) throws SQLException {
        checkCache(dataObject, CHECK_INSERT);
        checkClass(dataObject, VampiroDO.class, CHECK_INSERT);

        VampiroDO vampiroDO = (VampiroDO) dataObject;

        vampiroDO.setId(getNextId());

        StringBuffer strbuf = new StringBuffer();

        strbuf.append("INSERT INTO ");
        strbuf.append(getTableName());
        strbuf.append(" VALUES (");
        strbuf.append(vampiroDO.getId());
        strbuf.append(", ");
        strbuf.append(singleQuotes(vampiroDO.getNombre()));
        strbuf.append(", ");
        strbuf.append(singleQuotes(vampiroDO.getApellido()));
        strbuf.append(", ");
        strbuf.append(singleQuotes(vampiroDO.getCorreo()));
        strbuf.append(", ");
        strbuf.append(singleQuotes(vampiroDO.getFechaNac().toString()));
        strbuf.append(", ");
        strbuf.append(singleQuotes(vampiroDO.getContrasenia()));
        strbuf.append(", ");
        strbuf.append(singleQuotes(vampiroDO.getAlias()));
        strbuf.append(", ");

        // ------------------------------------------
        // Insert an image using a preparedStatement
        // ------------------------------------------

        strbuf.append("?, ");

        strbuf.append(vampiroDO.getFuerza());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getDefensa());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getSigilosidad());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getVelocidad());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getHabArmas());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getHabNegocios());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getPtosEntrenamiento());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getPtosExperiencia());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getSalud());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getSangre());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getDinero());
        strbuf.append(", ");
        strbuf.append(vampiroDO.getNumEsclavos());
        strbuf.append(", ");
        strbuf.append(vampiroDO.isActivo());
        strbuf.append(", ");

        // ---------------------------------
        // Setting the Reference to Familia
        // ---------------------------------

        Reference<IFamiliaDO> familiaRef = vampiroDO.getFamiliaId();
        familiaRef.checkInsert();

        strbuf.append(familiaRef.getIdAsString());
        strbuf.append(", ");

        // --------------------------------
        // Setting the Reference to Ciudad
        // --------------------------------

        Reference<ICiudadDO> ciudadRef = vampiroDO.getCiudadId();
        ciudadRef.checkInsert();

        strbuf.append(ciudadRef.getIdAsString());
        strbuf.append(")");

        // ---------------------------------------------------------------
        // The PreparedStatement is used to do the insertion of the image
        // ---------------------------------------------------------------

        PreparedStatement ps = connection.prepareStatement(strbuf.toString());
        ps.setBytes(1, vampiroDO.getImagen());
        ps.executeUpdate();
        ps.close();

        System.err.println(strbuf.toString());

        dtaSession.add(dataObject);
    }

    protected VampiroDO resultSetToDO(ResultSet rs) throws SQLException {
        VampiroDO ret = //
        (VampiroDO) dtaSession.getDtaByKey( //
                VampiroDO.class, rs.getInt(VampiroDO.ID));

        if (ret != null) {
            return ret;
        }

        ret = new VampiroDO();

        ret.setId/*     			*/(rs.getInt(VampiroDO.ID));
        ret.setNombre/*				*/(rs.getString(VampiroDO.NOMBRE));
        ret.setApellido/*			*/(rs.getString(VampiroDO.APELLIDO));
        ret.setCorreo/*				*/(rs.getString(VampiroDO.CORREO));
        ret.setContrasenia/*		*/(rs.getString(VampiroDO.CONTRASENIA));
        ret.setFechaNac/*			*/(rs.getDate(VampiroDO.FECHA_NAC));
        ret.setAlias/*				*/(rs.getString(VampiroDO.ALIAS));
        ret.setImagen/*				*/(rs.getBytes(VampiroDO.IMAGEN));
        ret.setFuerza/*				*/(rs.getInt(VampiroDO.FUERZA));
        ret.setDefensa/*			*/(rs.getInt(VampiroDO.DEFENSA));
        ret.setSigilosidad/*		*/(rs.getInt(VampiroDO.SIGILOSIDAD));
        ret.setVelocidad/*			*/(rs.getInt(VampiroDO.VELOCIDAD));
        ret.setHabArmas/*			*/(rs.getInt(VampiroDO.HAB_ARMAS));
        ret.setHabNegocios/*		*/(rs.getInt(VampiroDO.HAB_NEGOCIOS));
        ret.setPtosEntrenamiento/*	*/(rs.getInt(VampiroDO.PTOS_ENTRENAMIENTO));
        ret.setPtosExperiencia/*	*/(rs.getInt(VampiroDO.PTOS_EXPERIENCIA));
        ret.setSalud/*				*/(rs.getInt(VampiroDO.SALUD));
        ret.setSangre/*				*/(rs.getInt(VampiroDO.SANGRE));
        ret.setDinero/*				*/(rs.getFloat(VampiroDO.DINERO));
        ret.setNumEsclavos/*		*/(rs.getInt(VampiroDO.NUM_ESCLAVOS));
        ret.setActivo/*				*/(rs.getBoolean(VampiroDO.ACTIVO));

        // ----------------------------
        // Setting the reference id's
        // ----------------------------

        Reference<IFamiliaDO> familiaRef = new Reference<IFamiliaDO>();
        familiaRef.setRefIdent(rs.getInt(VampiroDO.FAMILIA_ID));

        // ------------
        // Familia Id
        // ------------
        ret.setFamiliaId(familiaRef);

        Reference<ICiudadDO> ciudadRef = new Reference<ICiudadDO>();
        ciudadRef.setRefIdent(rs.getInt(VampiroDO.CIUDAD_ID));

        // -----------
        // Ciudad Id
        // -----------

        ret.setCiudadId(ciudadRef);

        return (VampiroDO) dtaSession.add(ret);
    }

    @Override
    public void update(DataObject dataObject) throws SQLException {
        checkCache(dataObject, CHECK_UPDATE);
        checkClass(dataObject, VampiroDO.class, CHECK_UPDATE);

        VampiroDO vampiroDO = (VampiroDO) dataObject;

        StringBuffer strbuf = new StringBuffer();

        strbuf.append("UPDATE ");
        strbuf.append(getTableName());
        strbuf.append(" SET ");

        strbuf.append(VampiroDO.NOMBRE);
        strbuf.append(" = ");
        strbuf.append(singleQuotes(vampiroDO.getNombre()));

        strbuf.append(", ");

        strbuf.append(VampiroDO.APELLIDO);
        strbuf.append(" = ");
        strbuf.append(singleQuotes(vampiroDO.getApellido()));

        strbuf.append(", ");

        strbuf.append(VampiroDO.CORREO);
        strbuf.append(" = ");
        strbuf.append(singleQuotes(vampiroDO.getCorreo()));

        strbuf.append(", ");

        strbuf.append(VampiroDO.CONTRASENIA);
        strbuf.append(" = ");
        strbuf.append(singleQuotes(vampiroDO.getContrasenia()));

        strbuf.append(", ");

        strbuf.append(VampiroDO.FECHA_NAC);
        strbuf.append(" = ");
        strbuf.append(singleQuotes(vampiroDO.getFechaNac().toString()));

        strbuf.append(", ");

        strbuf.append(VampiroDO.ALIAS);
        strbuf.append(" = ");
        strbuf.append(singleQuotes(vampiroDO.getAlias()));

        strbuf.append(", ");

        strbuf.append(VampiroDO.IMAGEN);
        strbuf.append(" = ");
        strbuf.append(" ? ");

        strbuf.append(", ");

        strbuf.append(VampiroDO.FUERZA);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getFuerza());

        strbuf.append(", ");

        strbuf.append(VampiroDO.DEFENSA);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getDefensa());

        strbuf.append(", ");

        strbuf.append(VampiroDO.SIGILOSIDAD);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getSigilosidad());

        strbuf.append(", ");

        strbuf.append(VampiroDO.VELOCIDAD);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getVelocidad());

        strbuf.append(", ");

        strbuf.append(VampiroDO.HAB_ARMAS);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getHabArmas());

        strbuf.append(", ");

        strbuf.append(VampiroDO.HAB_NEGOCIOS);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getHabNegocios());

        strbuf.append(", ");

        strbuf.append(VampiroDO.PTOS_ENTRENAMIENTO);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getPtosEntrenamiento());

        strbuf.append(", ");

        strbuf.append(VampiroDO.PTOS_EXPERIENCIA);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getPtosExperiencia());

        strbuf.append(", ");

        strbuf.append(VampiroDO.SALUD);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getSalud());

        strbuf.append(", ");

        strbuf.append(VampiroDO.SANGRE);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getSangre());

        strbuf.append(", ");

        strbuf.append(VampiroDO.DINERO);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getDinero());

        strbuf.append(", ");

        strbuf.append(VampiroDO.NUM_ESCLAVOS);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getNumEsclavos());

        strbuf.append(", ");

        strbuf.append(VampiroDO.ACTIVO);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.isActivo());

        strbuf.append(", ");

        // ----------------------------
        // Set Familia Reference Id
        // ----------------------------

        Reference<IFamiliaDO> familiaRef = vampiroDO.getFamiliaId();
        familiaRef.checkUpdate();

        strbuf.append(VampiroDO.FAMILIA_ID);
        strbuf.append(" = ");
        strbuf.append(familiaRef.getIdAsString());

        strbuf.append(", ");

        // ----------------------------
        // Set Ciudad Reference Id
        // ----------------------------

        Reference<ICiudadDO> ciudadRef = vampiroDO.getCiudadId();
        ciudadRef.checkUpdate();

        strbuf.append(VampiroDO.CIUDAD_ID);
        strbuf.append(" = ");
        strbuf.append(ciudadRef.getIdAsString());

        strbuf.append(" WHERE ");
        strbuf.append(VampiroDO.ID);
        strbuf.append(" = ");
        strbuf.append(vampiroDO.getId());

        PreparedStatement ps = connection.prepareStatement(strbuf.toString());
        ps.setBytes(1, vampiroDO.getImagen());

        ps.executeUpdate();
        ps.close();

        System.err.println(strbuf.toString());
    }

    public void loadVisitaList(IVampiroDO vampiroDO) throws Exception {
        checkCache(vampiroDO, CHECK_UPDATE);

        IVisitaDAO visitaDAO = (IVisitaDAO) FactoryDAO.getDAO( //
                VisitaDAO.class, connectionBean);

        vampiroDO.setVisitaList(visitaDAO.listByVampiroId(vampiroDO.getId()));
    }

    public void loadVampiroArticuloList(IVampiroDO vampiroDO) throws Exception {
        checkCache(vampiroDO, CHECK_UPDATE);

        IVampiroArticuloDAO vampiroArticuloDAO = (IVampiroArticuloDAO) FactoryDAO.getDAO( //
                VampiroArticuloDAO.class, connectionBean);

        vampiroDO.setVampiroArticuloList(vampiroArticuloDAO.listByVampiroId(vampiroDO.getId()));
    }

    public void loadPropiedadVampiroList(IVampiroDO vampiroDO) throws Exception {
        checkCache(vampiroDO, CHECK_UPDATE);

        IPropiedadVampiroDAO propiedadVampiroDAO = (IPropiedadVampiroDAO) FactoryDAO.getDAO( //
                PropiedadVampiroDAO.class, connectionBean);

        vampiroDO.setPropiedadVampiroList(propiedadVampiroDAO.listByVampiroId(vampiroDO.getId()));
    }

    public void loadFamiliaRef(IVampiroDO vampiroDO) throws SQLException {
        checkClass(vampiroDO, VampiroDO.class, CHECK_UPDATE);

        FamiliaDAO familiaDAO = new FamiliaDAO();
        familiaDAO.init(connectionBean);

        Reference<IFamiliaDO> ref = vampiroDO.getFamiliaId();

        // ----------------------------------------
        // If ident == 0 there is nothing to do
        // ----------------------------------------

        if (ref.getRefIdent() == 0) {
            return;
        }

        FamiliaDO familiaDO = //
        (FamiliaDO) familiaDAO.loadById(ref.getRefIdent());

        ref.setRefValue(familiaDO);
    }

    public void loadCiudadRef(IVampiroDO vampiroDO) throws SQLException {
        checkClass(vampiroDO, VampiroDO.class, CHECK_UPDATE);

        CiudadDAO ciudadDAO = new CiudadDAO();
        ciudadDAO.init(connectionBean);

        Reference<ICiudadDO> ref = vampiroDO.getCiudadId();

        // ----------------------------------------
        // If ident == 0 there is nothing to do
        // ----------------------------------------

        if (ref.getRefIdent() == 0) {
            return;
        }

        CiudadDO ciudadDO = //
        (CiudadDO) ciudadDAO.loadById(ref.getRefIdent());

        ref.setRefValue(ciudadDO);
    }

    public List<IVampiroDO> listByCiudadId(int ciudadId) throws SQLException {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT * FROM ");
        strbuf.append(getTableName());

        strbuf.append(" WHERE ");
        strbuf.append(VampiroDO.CIUDAD_ID);
        strbuf.append(" = ");
        strbuf.append(ciudadId);

        System.err.println(strbuf.toString());

        ResultSet rs = //
        connection.createStatement().executeQuery(strbuf.toString());

        List<IVampiroDO> ret = new ArrayList<IVampiroDO>();

        while (rs.next()) {
            ret.add(resultSetToDO(rs));
        }

        return ret;
    }

    public List<IVampiroDO> listByFamiliaId(int familiaId) throws SQLException {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT * FROM ");
        strbuf.append(getTableName());

        strbuf.append(" WHERE ");
        strbuf.append(VampiroDO.FAMILIA_ID);
        strbuf.append(" = ");
        strbuf.append(familiaId);

        System.err.println(strbuf.toString());

        ResultSet rs = //
        connection.createStatement().executeQuery(strbuf.toString());

        List<IVampiroDO> ret = new ArrayList<IVampiroDO>();

        while (rs.next()) {
            ret.add(resultSetToDO(rs));
        }

        return ret;
    }

    public List<IVampiroDO> listAllOrderedByCiudad() throws SQLException {
        return listAllOrderedByCiudad(-1, -1);
    }

    public List<IVampiroDO> listAllOrderedByCiudad(int lim, int off) throws SQLException {
        StringBuffer strbuf = new StringBuffer();

        CiudadDAO ciudadDAO = new CiudadDAO();

        strbuf.append("SELECT ");
        strbuf.append(getTableName() + ".*");
        strbuf.append(" FROM ");
        strbuf.append(getTableName());
        strbuf.append(" JOIN ");
        strbuf.append(ciudadDAO.getTableName());
        strbuf.append(" ON ");
        strbuf.append(getTableName() + "." + VampiroDO.CIUDAD_ID);
        strbuf.append(" = ");
        strbuf.append(ciudadDAO.getTableName() + "." + CiudadDO.ID);
        strbuf.append(" ORDER BY ");
        strbuf.append(ciudadDAO.getTableName() + "." + CiudadDO.NOMBRE);

        if (lim >= 0 && off >= 0) {
            strbuf.append(" LIMIT  ");
            strbuf.append(lim);
            strbuf.append(" OFFSET ");
            strbuf.append(off);
        }

        System.err.println(strbuf.toString());

        ResultSet rs = //
        connection.createStatement().executeQuery(strbuf.toString());

        List<IVampiroDO> ret = new ArrayList<IVampiroDO>();

        while (rs.next()) {
            ret.add(resultSetToDO(rs));
        }

        return ret;
    }

    public List<IVampiroDO> listAllOrderedByPtosExperiencia() throws SQLException {
        return listAllOrderedByPtosExperiencia(-1, -1);
    }

    public List<IVampiroDO> listAllOrderedByPtosExperiencia(int lim, int off) throws SQLException {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT * FROM ");
        strbuf.append(getTableName());
        strbuf.append(" ORDER BY ");
        strbuf.append(VampiroDO.PTOS_EXPERIENCIA);
        strbuf.append(" DESC ");

        if (lim >= 0 && off >= 0) {
            strbuf.append(" LIMIT  ");
            strbuf.append(lim);
            strbuf.append(" OFFSET ");
            strbuf.append(off);
        }

        System.err.println(strbuf.toString());

        ResultSet rs = //
        connection.createStatement().executeQuery(strbuf.toString());

        List<IVampiroDO> ret = new ArrayList<IVampiroDO>();

        while (rs.next()) {
            ret.add(resultSetToDO(rs));
        }

        return ret;
    }

    public IVampiroDO loadByAlias(String alias) throws SQLException {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT * FROM ");
        strbuf.append(getTableName());

        strbuf.append(" WHERE ");
        strbuf.append(VampiroDO.ALIAS);
        strbuf.append(" = ");
        strbuf.append(singleQuotes(alias));

        System.err.println(strbuf.toString());

        ResultSet rs = connection.createStatement().executeQuery(strbuf.toString());

        if (rs.next()) {
            return resultSetToDO(rs);
        }

        return null;
    }

    public IVampiroDO loadByCorreo(String correo) throws SQLException {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT * FROM ");
        strbuf.append(getTableName());

        strbuf.append(" WHERE ");
        strbuf.append(VampiroDO.CORREO);
        strbuf.append(" = ");
        strbuf.append(singleQuotes(correo));

        System.err.println(strbuf.toString());

        ResultSet rs = connection.createStatement().executeQuery(strbuf.toString());

        if (rs.next()) {
            return resultSetToDO(rs);
        }

        return null;
    }

    @Override
    public List<IVampiroDO> listByAliasLike(String alias) throws SQLException {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT ");
        strbuf.append("*");
        strbuf.append(" FROM ");
        strbuf.append(getTableName());
        strbuf.append(" WHERE ");
        strbuf.append(VampiroDO.ALIAS);
        strbuf.append(" LIKE ");
        strbuf.append(singleQuotes("%" + alias + "%"));

        System.err.println(strbuf.toString());

        ResultSet rs = //
        connection.createStatement().executeQuery(strbuf.toString());

        List<IVampiroDO> ret = new ArrayList<IVampiroDO>();

        while (rs.next()) {
            ret.add(resultSetToDO(rs));
        }

        return ret;
    }

    @Override
    public byte[] loadImageByVampiroId(int vampiroId) throws SQLException {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT ");
        strbuf.append(VampiroDO.IMAGEN);
        strbuf.append(" FROM ");
        strbuf.append(getTableName());
        strbuf.append(" WHERE ");
        strbuf.append(VampiroDO.ID);
        strbuf.append(" = ");
        strbuf.append(vampiroId);

        System.err.println(strbuf.toString());

        ResultSet rs = //
        connection.createStatement().executeQuery(strbuf.toString());

        if (rs.next()) {
            return rs.getBytes(VampiroDO.IMAGEN);
        }

        return null;
    }
}
